package com.btpn.custody.web.m005.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.bean.ParameterBeans;
import com.btpn.custody.bean.SchedulerBeans;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_settingSLA")
})
@Validations(
	intRangeFields = {
		@IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "slaParam", min = "1", max = "5", message = "Parameter SLA antara ${min} - ${max}")
	}
)
public class SettingParameter extends ActionSupport {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 4136208967385664800L;

	/*=============================================== M E M B E R =============================================*/	
	@Autowired
	private SchedulerBeans schedulerBeans;
	
	@Autowired
	private ParameterBeans parameterBeans;
	
	private Integer slaParam;
		
	/*=============================================== B E H A V I O R =========================================*/
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value = "/settingSLA-form", results = { @Result(name="success", location="t_settingSLA") })
	public String showSLASetting() {
		slaParam = parameterBeans.getSLAparam();
		return SUCCESS;
	}
	
	/*
	 * update parameter for SLA
	 * by admin
	 */
	@Action(value = "/updateParamSLA", results = { @Result(name="success", location="t_settingSLA") })
	public String saveParameterSLA() {

		Integer result = parameterBeans.updateSLAparam(slaParam);
		if (result!=null) {
			slaParam = result;
			addActionMessage("success update SLA");
		} else {
			addActionError("fail update SLA");
		}
		
		return SUCCESS;
	}
	
	/*
	 * refresh all parameter for docs
	 * by admin
	 */
	@Action(value = "/refreshParameter", results = { 
		@Result(name="success", location="t_settingSLA") 
	})
	public String refreshParameter() {

		if (schedulerBeans.refreshDownloadParameter(SecurityContextHolder.getContext().getAuthentication().getName())) {
			addActionMessage("Success refresh parameter");
		} else {
			addActionError("Fail refresh Parameter");
		}

		slaParam = parameterBeans.getSLAparam();
					
		return SUCCESS;
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public Integer getSlaParam() {
		return slaParam;
	}

	public void setSlaParam(Integer slaParam) {
		this.slaParam = slaParam;
	}
}
